package com.csxy.qiu.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csxy.qiu.JwtUtils;
import com.csxy.qiu.MD5;
import com.csxy.qiu.R;
import com.csxy.qiu.entity.UcenterMember;
import com.csxy.qiu.entity.vo.LoginVo;
import com.csxy.qiu.entity.vo.RegisterVo;
import com.csxy.qiu.exception.MyException;
import com.csxy.qiu.mapper.UcenterMapper;
import com.csxy.qiu.service.UcenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class UcenterServiceImpl extends ServiceImpl<UcenterMapper, UcenterMember> implements UcenterService {

    // 登录
    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getPhone();
        String password = loginVo.getPassword();
        if (StringUtils.isEmpty(mobile) && StringUtils.isEmpty(password)){
            throw new MyException(20001,"手机号和密码不能为空");
        }
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile",mobile);
        UcenterMember member = baseMapper.selectOne(queryWrapper);
        if (member == null ){
            throw new MyException(20001,"手机号已存在");
        }

        //加密算法验证密码
        if (!MD5.encrypt(password).equals(member.getPassword())){
            throw new MyException(20001,"密码错误");
        }

        //判断用户是否禁用
        if (member.getIsDisabled()){
            throw new MyException(20001,"用户被禁用");

        }
        //生成jwtToken
        String token = JwtUtils.getJwtToken(member.getId(),member.getNickname());
        return token;


    }


    //注册
    @Override
    public void register(RegisterVo registerVo) {
        //获取前端传来的数据
        String nickname = registerVo.getNickname();//昵称
        String code = registerVo.getCode();//验证码
        String mobile = registerVo.getMobile();//手机号
        String password = registerVo.getPassword();//密码

        //非空判断
        if (StringUtils.isEmpty(nickname)
                || StringUtils.isEmpty(code)
                || StringUtils.isEmpty(mobile)
                || StringUtils.isEmpty(password)) {
            throw new MyException(20001, "传来的数据有空值，注册失败");
        }
        //验证码判断
        if (!code.equals("7777")) {
            throw new MyException(20001, "验证码不正确，注册失败");
        }
        //手机号判断
        QueryWrapper<UcenterMember> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("mobile", mobile);
        Long count = baseMapper.selectCount(queryWrapper);
        if (count > 0) {
            throw new MyException(20001, "手机号重复，注册失败");
        }
        //数据添加到数据库中
        UcenterMember member = new UcenterMember();
        member.setPassword(MD5.encrypt(password));//密码加密
        member.setMobile(mobile);
        member.setNickname(nickname);
        member.setIsDisabled(false);//用户不禁用
        member.setAvatar("https://img.jianbihua.com/sites/default/files/styles/photo640x425/public/images/2018-10/20181029170422_687088.jpg");
        baseMapper.insert(member);
    }

    //统计某一天注册人数
    @Autowired
    private UcenterMapper ucenterMapper;
    @Override
    public Integer getCountRegister(String day) {
        return ucenterMapper.getCountRegister(day);
    }
}
